﻿using System;
using System.Collections;
using System.Data;

using CE.Database;
/// <summary>
/// Summary description for UserProfileDB
/// </summary>
/// 
namespace CE.User
{
    public class UserSelectHistoryDB
    {
        public UserSelectHistoryDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public void LogUserSelectHistory(string PageType, string BrowserType, string IPNumber, string SelectedType, Int64 SelectedResultId, Int64 SelectedCompanyId)
        {
            UserSelectHistory _userselect = new UserSelectHistory();
            UserViewHistory _userview = new UserViewHistory();
            UserViewHistoryDB _userviewdb = new UserViewHistoryDB();

            string _selectDate = DateTime.Now.ToString("yyyyMMdd");
            Int64 _viewedId = 0;
            byte _viewedno = 0;

            _userview = _userviewdb.GetUserViewHistory(IPNumber, PageType, BrowserType, _selectDate);
            if (_userview != null)
            {
                _viewedId = _userview.ViewedId;
                _viewedno = _userview.ViewedCount;
            }

            _userselect = GetUserSelectHistory(_selectDate, SelectedCompanyId, SelectedType, _viewedId, _viewedno);
            if (_userselect == null)
            {
                _userselect = new UserSelectHistory();
                _userselect.SelectedType = SelectedType;
                _userselect.SelectedResultId = SelectedResultId;
                _userselect.SelectedCompanyId = SelectedCompanyId;
                _userselect.SelectedCount = 1;
                _userselect.ViewedId = _viewedId;
                _userselect.ViewedNo = _viewedno;
                Insert(_userselect);
            }
            else
            {
                _userselect.SelectedCount += 1;
                Insert(_userselect);
            }

        }

        public Int64 Insert(UserSelectHistory userselect)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocUserSelectedHistoryInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@SelectedId", userselect.SelectedId);
            _sPara.Add("@SelectedType", userselect.SelectedType);
            _sPara.Add("@SelectedResultId", userselect.SelectedResultId);
            _sPara.Add("@SelectedCompanyId", userselect.SelectedCompanyId);
            _sPara.Add("@ViewedId", userselect.ViewedId);
            _sPara.Add("@ViewedNo", userselect.ViewedNo);
            _sPara.Add("@SelectedCount", userselect.SelectedCount);
            _sPara.Add("@Info1", userselect.Info1);
            _sPara.Add("@Info2", userselect.Info2);
            _sPara.Add("@Info3", userselect.Info3);
            _sPara.Add("@Info4", userselect.Info4);
            _sPara.Add("@Info5", userselect.Info5);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public UserSelectHistoryList GetUserSelectHistoryListByCompanyId(Int64 SelectedCompanyId)
        {
            UserSelectHistoryList _mySelecthistorylist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocUserSelectedHistorySelectItemListByCompanyId";

            SortedList _spara = new SortedList();
            _spara.Add("@SelectedCompanyId", SelectedCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _mySelecthistorylist = new UserSelectHistoryList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        UserSelectHistory _mySelecthistory = new UserSelectHistory();
                        _mySelecthistory = FillDataRecord(_dt, i);
                        _mySelecthistorylist.Add(_mySelecthistory);
                    }
                }
            }
            return _mySelecthistorylist;
        }

        public UserSelectHistoryList GetUserSelectHistoryListBySelectedDate(string SelectedDate)
        {
            UserSelectHistoryList _mySelecthistorylist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocUserSelectedHistorySelectItemListBySelectedDate";

            SortedList _spara = new SortedList();
            _spara.Add("@SelectedDate", SelectedDate);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _mySelecthistorylist = new UserSelectHistoryList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        UserSelectHistory _mySelecthistory = new UserSelectHistory();
                        _mySelecthistory = FillDataRecord(_dt, i);
                        _mySelecthistorylist.Add(_mySelecthistory);
                    }
                }
            }
            return _mySelecthistorylist;
        }

        public UserSelectHistoryList GetUserSelectHistoryListBySelectedDateCompanyId(string SelectedDate, Int64 SelectedCompanyId)
        {
            UserSelectHistoryList _mySelecthistorylist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocUserSelectedHistorySelectItemListBySelectedDateCompanyId";

            SortedList _spara = new SortedList();
            _spara.Add("@SelectedDate", SelectedDate);
            _spara.Add("@SelectedCompanyId", SelectedCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _mySelecthistorylist = new UserSelectHistoryList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        UserSelectHistory _mySelecthistory = new UserSelectHistory();
                        _mySelecthistory = FillDataRecord(_dt, i);
                        _mySelecthistorylist.Add(_mySelecthistory);
                    }
                }
            }
            return _mySelecthistorylist;
        }

        public UserSelectHistory GetUserSelectHistory(string SelectedDate, Int64 SelectedCompanyId, string SelectedType, Int64 ViewedId, byte ViewedNo)
        {
            UserSelectHistory _Selecthistory = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocUserSelectedHistorySelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@SelectedDate", SelectedDate);
            _spara.Add("@SelectedCompanyId", SelectedCompanyId);
            _spara.Add("@SelectedType", SelectedType);
            _spara.Add("@ViewedId", ViewedId);
            _spara.Add("@ViewedNo", ViewedNo);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _Selecthistory = new UserSelectHistory();
                    _Selecthistory = FillDataRecord(_dt, 0);
                }
            }
            return _Selecthistory;
        }

        private UserSelectHistory FillDataRecord(DataTable myDataTable, int RowId)
        {
            UserSelectHistory myUserSelect = new UserSelectHistory();
            myUserSelect.SelectedId = Convert.ToInt64(myDataTable.Rows[RowId]["SelectedId"].ToString());
            myUserSelect.SelectedResultId = Convert.ToInt64(myDataTable.Rows[RowId]["SelectedResultId"].ToString());
            myUserSelect.SelectedCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["SelectedCompanyId"].ToString());
            myUserSelect.SelectedCount = Convert.ToByte(myDataTable.Rows[RowId]["SelectedCount"].ToString());
            myUserSelect.ViewedId = Convert.ToInt64(myDataTable.Rows[RowId]["ViewedId"].ToString());
            myUserSelect.SelectedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["SelectedDate"].ToString());
            myUserSelect.Info1 = myDataTable.Rows[RowId]["Info1"].ToString();
            myUserSelect.Info2 = myDataTable.Rows[RowId]["Info2"].ToString();
            myUserSelect.Info3 = myDataTable.Rows[RowId]["Info3"].ToString();
            myUserSelect.Info4 = myDataTable.Rows[RowId]["Info4"].ToString();
            myUserSelect.Info5 = myDataTable.Rows[RowId]["Info5"].ToString();
            return myUserSelect;
        }

        public bool Update()
        {
            return true;
        }

        public bool Delete()
        {
            return true;
        }

    }
}